/*******************************************************************************
 * Copyright (c) 2011-2012 TXT e-solutions SpA
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the research area ICT-2009.1.3
 * Internet of Things and enterprise environments.
 *
 *
 * Authors:
 *     Cristoforo Seccia (TXT e-solutions SpA)
 *
 * Contributors:
 *      Domenico Rotondi (TXT e-solutions SpA)
 *******************************************************************************/
package it.txt.access.capability.wizard.panel;

import it.txt.access.capability.applications.wizard.commons.XMLViewerDialog;
import it.txt.access.capability.commons.filter.FilterXML;
import it.txt.access.capability.wizard.panel.controller.ControllerPanelAssertion;
import java.awt.Frame;
import java.io.File;
import java.net.URI;
import javax.swing.JFileChooser;

/**
 *
 * @author Cristoforo Seccia (TXT e-solutions SpA)
 */
public class PanelAssertion extends javax.swing.JPanel {

    private static final long serialVersionUID = -1303717156947414279L;
    private XMLViewerDialog rightsViewerDialog;
    private JFileChooser accessRightsSaveFileChooser;
    private File currentDelegationDirecotory = null;
    private ControllerPanelAssertion controller;
    private FilterXML filterAssertion;
    private Frame myContainer;

    /** Creates new form WelcomePanel */
    public PanelAssertion(Frame container, ControllerPanelAssertion cpa) {
        initComponents();
        controller = cpa;
        myContainer = container;
        filterAssertion = new FilterXML("Access Rights Capability");
        accessRightsSaveFileChooser = new JFileChooser();
        accessRightsSaveFileChooser.setFileFilter(filterAssertion);
        accessRightsSaveFileChooser.setMultiSelectionEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        centerPanel = new javax.swing.JPanel();
        centerTitlePanel = new javax.swing.JPanel();
        shortDescrLabel = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        welcomeLabel = new javax.swing.JLabel();
        progressSent = new javax.swing.JProgressBar();
        progressDescription = new javax.swing.JLabel();
        assertionCreatedPanel = new javax.swing.JPanel();
        saveAssertionPanel = new javax.swing.JPanel();
        viewAccessRightsLabel = new javax.swing.JLabel();
        saveAcessRightsLabel = new javax.swing.JLabel();
        questionLabel = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        viewAccessRightsButton = new javax.swing.JButton();
        saveAccessRightsButton = new javax.swing.JButton();

        centerPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        centerTitlePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("IoT@Work - Capability Based Authorization"));

        shortDescrLabel.setText("Wait for tha Assertion to be created.");

        welcomeLabel.setText("Access Capability");

        progressSent.setStringPainted(true);

        progressDescription.setText("Creating Assertion...");

        assertionCreatedPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        saveAssertionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Access Rights Capability created"));

        viewAccessRightsLabel.setText("View your Access Rights Capability Assertion");

        saveAcessRightsLabel.setText("Save your Access Rights Capability Assertion");

        questionLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        questionLabel.setText("What do you want to do now?");

        viewAccessRightsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons_24x24/Preview_24x24.png"))); // NOI18N
        viewAccessRightsButton.setText("View");
        viewAccessRightsButton.setEnabled(false);
        viewAccessRightsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewAccessRightsButtonActionPerformed(evt);
            }
        });

        saveAccessRightsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons_24x24/Save_24x24.png"))); // NOI18N
        saveAccessRightsButton.setText("Save");
        saveAccessRightsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveAccessRightsButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout saveAssertionPanelLayout = new javax.swing.GroupLayout(saveAssertionPanel);
        saveAssertionPanel.setLayout(saveAssertionPanelLayout);
        saveAssertionPanelLayout.setHorizontalGroup(
            saveAssertionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(saveAssertionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(saveAssertionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(questionLabel)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
                    .addGroup(saveAssertionPanelLayout.createSequentialGroup()
                        .addGroup(saveAssertionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(saveAcessRightsLabel)
                            .addComponent(viewAccessRightsLabel))
                        .addGap(18, 18, 18)
                        .addGroup(saveAssertionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(viewAccessRightsButton)
                            .addComponent(saveAccessRightsButton))))
                .addContainerGap())
        );
        saveAssertionPanelLayout.setVerticalGroup(
            saveAssertionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(saveAssertionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(questionLabel)
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(saveAssertionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveAccessRightsButton)
                    .addComponent(saveAcessRightsLabel))
                .addGap(18, 18, 18)
                .addGroup(saveAssertionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(viewAccessRightsButton)
                    .addComponent(viewAccessRightsLabel))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout assertionCreatedPanelLayout = new javax.swing.GroupLayout(assertionCreatedPanel);
        assertionCreatedPanel.setLayout(assertionCreatedPanelLayout);
        assertionCreatedPanelLayout.setHorizontalGroup(
            assertionCreatedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(assertionCreatedPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(saveAssertionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        assertionCreatedPanelLayout.setVerticalGroup(
            assertionCreatedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(assertionCreatedPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(saveAssertionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout centerTitlePanelLayout = new javax.swing.GroupLayout(centerTitlePanel);
        centerTitlePanel.setLayout(centerTitlePanelLayout);
        centerTitlePanelLayout.setHorizontalGroup(
            centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, centerTitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(assertionCreatedPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
                    .addComponent(welcomeLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
                    .addComponent(shortDescrLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
                    .addComponent(progressDescription, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
                    .addComponent(progressSent, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE))
                .addContainerGap())
        );
        centerTitlePanelLayout.setVerticalGroup(
            centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerTitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(welcomeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(shortDescrLabel)
                .addGap(18, 18, 18)
                .addComponent(progressSent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(progressDescription)
                .addGap(18, 18, 18)
                .addComponent(assertionCreatedPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout centerPanelLayout = new javax.swing.GroupLayout(centerPanel);
        centerPanel.setLayout(centerPanelLayout);
        centerPanelLayout.setHorizontalGroup(
            centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(centerTitlePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        centerPanelLayout.setVerticalGroup(
            centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(centerTitlePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(centerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(centerPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void viewAccessRightsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewAccessRightsButtonActionPerformed
        handleViewAccessRightsButtonActionPerformed();
    }//GEN-LAST:event_viewAccessRightsButtonActionPerformed

    private void saveAccessRightsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAccessRightsButtonActionPerformed
        handleSaveAccessRightsButtonActionPerformed();
    }//GEN-LAST:event_saveAccessRightsButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel assertionCreatedPanel;
    private javax.swing.JPanel centerPanel;
    private javax.swing.JPanel centerTitlePanel;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel progressDescription;
    private javax.swing.JProgressBar progressSent;
    private javax.swing.JLabel questionLabel;
    private javax.swing.JButton saveAccessRightsButton;
    private javax.swing.JLabel saveAcessRightsLabel;
    private javax.swing.JPanel saveAssertionPanel;
    private javax.swing.JLabel shortDescrLabel;
    private javax.swing.JButton viewAccessRightsButton;
    private javax.swing.JLabel viewAccessRightsLabel;
    private javax.swing.JLabel welcomeLabel;
    // End of variables declaration//GEN-END:variables

    private void handleViewAccessRightsButtonActionPerformed(){
        try {
            URI tokenUri = controller.getSavedAccessRightCapabilityPath();
            rightsViewerDialog = new XMLViewerDialog(tokenUri, controller.getAccessRightsCapabilityToken(), myContainer, true);
            rightsViewerDialog.setLocationRelativeTo(myContainer);
            rightsViewerDialog.setVisible(true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void handleSaveAccessRightsButtonActionPerformed() {
        if (currentDelegationDirecotory == null) {
            accessRightsSaveFileChooser.setCurrentDirectory(new File("."));
        }
        else {
            accessRightsSaveFileChooser.setCurrentDirectory(currentDelegationDirecotory);
        }
        int returnValue = accessRightsSaveFileChooser.showSaveDialog(myContainer);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File assertionFile = accessRightsSaveFileChooser.getSelectedFile();
            if(controller.saveSignedAccessRightCapabilityFile(assertionFile)){
                viewAccessRightsButton.setEnabled(true);
            }
            else {
                viewAccessRightsButton.setEnabled(false);
            }
        }
    }

    public void setProgressText(String s) {
        progressDescription.setText(s);
    }

    public void setProgressValue(int i) {
        if(i == 100){
            saveAssertionPanel.setVisible(true);
        }
        else{
            saveAssertionPanel.setVisible(false);
        }
        progressSent.setValue(i);
    }
}
